class ProfilesController < ApplicationController
	before_filter :require_user, :only => [:show, :edit, :update, :picture, :password]
	load_and_authorize_resource :through => :account

  # GET /profiles/1
  # GET /profiles/1.xml
  def show
    @profile = current_user.profile

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @profile }
    end
  end

	def view
		@profile = Profile.find(params[:id])
		@comment = Comment.new
	end

  # GET /profiles/new
  # GET /profiles/new.xml
  #def new
  #  @profile = Profile.new

  #  respond_to do |format|
  #    format.html # new.html.erb
  #    format.xml  { render :xml => @profile }
  #  end
  #end

  # GET /profiles/1/edit
  def edit
    @profile = current_user.profile
  end

  # GET /profiles/1/password
  def password
    @user = current_user
  end

  # POST /profiles
  # POST /profiles.xml
  def create
    @profile = Profile.new(params[:profile])

    respond_to do |format|
      if @profile.save
        flash[:notice] = 'Profile was successfully created.'
        format.html { redirect_to(@profile) }
        format.xml  { render :xml => @profile, :status => :created, :location => @profile }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /profiles/1
  # PUT /profiles/1.xml
  def update
    @profile = current_user.profile

    respond_to do |format|
			if params[:profile][:image_data]
				@profile.remove_images
				@profile.save
			end
      if @profile.update_attributes(params[:profile])
        flash[:notice] = 'Profile was successfully updated.'
        format.html { redirect_to(account_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end

	def picture
		@profile = current_user.profile
	end

end
